package com.china08.yxyapi.api.manage.tec.office.attendance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendanceStatistics;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.attendance.YxyTeaAttendanceStatisticsRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教师考勤--我的考勤--考勤记录
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class AttendanceYxyTeaAttendanceStatisticsSrhController extends YxyApiBaseController {

    @Autowired
    private YxyTeaAttendanceStatisticsRepository yxyTeaAttendanceStatisticsRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/attendance/YxyTeaAttendanceStatisticsSrh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel, RespModel respModel) throws ParseException {

        String teacherId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        // 格式化日期、类型转换
        Integer dayOfDate = reqModel.dayOfDate;
        if (dayOfDate == null) {
            Assert.isTrue(false, "选择月份不能为空！");
        }
        SimpleDateFormat dfDateFormat = new SimpleDateFormat("yyyyMM");
        Date date04 = dfDateFormat.parse(dayOfDate + "");

        // 本月月份+1
        Calendar calender = Calendar.getInstance();
        calender.setTime(date04);
        calender.add(Calendar.MONTH, 1);
        Date date05 = calender.getTime();

        Integer intDate05 = Integer.parseInt(dfDateFormat.format(date05));
        List<RespModel> returnList = new ArrayList<>();
        List<YxyTeaAttendanceStatistics> yxyTeaAttendanceStatisticsLst = yxyTeaAttendanceStatisticsRepository
                .findByTeacherIdAndSchoolIdAndDayOfDateBetweenOrderByDayOfDateDesc(teacherId, schoolId, dayOfDate * 100,
                        intDate05 * 100);

        for (YxyTeaAttendanceStatistics yxyTeaAttendanceStatistics : yxyTeaAttendanceStatisticsLst) {

            String dayOfDate1 = yxyTeaAttendanceStatistics.getDayOfDate() + "";
            String d1 = StringUtils.substring(dayOfDate1, 0, 4);
            String d2 = StringUtils.substring(dayOfDate1, 4, 6);
            String d3 = StringUtils.substring(dayOfDate1, 6, 8);
            dayOfDate1 = d1 + "-" + d2 + "-" + d3;

            boolean isHoliday = yxyTeaAttendanceStatistics.getIsHoliday() == 1;
            String description = yxyTeaAttendanceStatistics.getHolidayDescription();

            Date signTime1 = yxyTeaAttendanceStatistics.getSignTime1();
            Date signTime2 = yxyTeaAttendanceStatistics.getSignTime2();
            Date signTime3 = yxyTeaAttendanceStatistics.getSignTime3();
            Date signTime4 = yxyTeaAttendanceStatistics.getSignTime4();

            String t1 = DateToolUtils.format(signTime1, "HH:mm");
            String t2 = DateToolUtils.format(signTime2, "HH:mm");
            String t3 = DateToolUtils.format(signTime3, "HH:mm");
            String t4 = DateToolUtils.format(signTime4, "HH:mm");

            RespModel model = new RespModel();

            model.signTime1 = t1;
            model.signTime2 = t2;
            model.signTime3 = t3;
            model.signTime4 = t4;
            model.isHoliday = isHoliday;
            model.description = description;

            model.dayOfDate = dayOfDate1;
            model.teacherName = yxyTeaAttendanceStatistics.getTeacherName();
            model.schoolName = yxyTeaAttendanceStatistics.getSchoolName();
            model.times = yxyTeaAttendanceStatistics.getTimes();
            model.weekOfDate = yxyTeaAttendanceStatistics.getWeekOfDate();
            model.signStatus1 = yxyTeaAttendanceStatistics.getSignStatus1();
            model.signStatus2 = yxyTeaAttendanceStatistics.getSignStatus2();
            model.signStatus3 = yxyTeaAttendanceStatistics.getSignStatus3();
            model.signStatus4 = yxyTeaAttendanceStatistics.getSignStatus4();
            model.signStatus5 = yxyTeaAttendanceStatistics.getSignStatus5();
            model.signTime5 = yxyTeaAttendanceStatistics.getSignTime5();
            model.signStatus6 = yxyTeaAttendanceStatistics.getSignStatus6();
            model.signTime6 = yxyTeaAttendanceStatistics.getSignTime6();
            model.signStatus7 = yxyTeaAttendanceStatistics.getSignStatus7();
            model.signTime7 = yxyTeaAttendanceStatistics.getSignTime7();
            model.signStatus8 = yxyTeaAttendanceStatistics.getSignStatus8();
            model.signTime8 = yxyTeaAttendanceStatistics.getSignTime8();
            returnList.add(model);
        }
        return returnList;

    }

    protected static class ReqModel extends YxyApiReqModel {

        // 前台点击按钮选择月份
        public Integer dayOfDate;

        // 教师id
        public String teacherId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 签到日期
        public String dayOfDate;
        // 老师姓名
        public String teacherName;
        // 学校名称
        public String schoolName;
        // 打卡次数
        public String times;
        // 星期几
        public String weekOfDate;
        // 第一次签到时间
        public String signTime1;
        // 第一次签到状态
        public Integer signStatus1 = 0;
        // 第二次签到时间
        public String signTime2;
        // 第二次签到状态
        public Integer signStatus2 = 0;
        public String signTime3;
        public Integer signStatus3 = 0;
        public String signTime4;
        public Integer signStatus4 = 0;
        public Date signTime5;
        public Integer signStatus5 = 0;
        public Date signTime6;
        public Integer signStatus6 = 0;
        public Date signTime7;
        public Integer signStatus7 = 0;
        public Date signTime8;
        public Integer signStatus8 = 0;
        public Boolean isHoliday;
        public String description;
    }
}
